#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/07/13 下午9:20
# @Author  : twl
import requests
import json
import sys
import datetime
#reload(sys)
#sys.setdefaultencoding('utf8')
from openpyxl import Workbook
username = "tanwenliang@coocaa.com"
password = "aaaaaa"
s = requests.Session()
url = "http://mirror.mgr.report.coocaatv.com/"
loginPath = "login"
detailPath = "/device/adevice/getEditParams"
listPath = "/manage/user/get_users_json"
def doLogin():
    header = {
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36"
    }
    data = {
        "loginName": username,
        "password": password
    }
    api = url + loginPath
    s = requests.Session()
    r = s.post(api, data)
    return s;

def reqUrl(session):
    url =  "http://mirror.mgr.report.coocaatv.com/manage/user/get_users_json?userMsg=&currentPage=1&pageSize=1000"
    response = session.request("GET", url, headers={})
    a = None
    try:
        a = json.loads(response.text)
        print (a)
    except ValueError:
        print ('Decoding JSON has failed')
    return a

if __name__ == '__main__':
    wb = Workbook()
    ws = wb.active
    ws.append([
        "账号","名称","归属机构","角色","时间","状态"
    ])
    session = doLogin()

    res = reqUrl(session)
    print (1)
    for item in res["lists"]:

        row = []
        row.append(item["loginName"])
        row.append(item["name"])
        row.append(item["officeName"])
        row.append(item["roleNameStr"])
        row.append(item["createDate"])
        if item["loginFlag"] ==1:
            row.append("启用")
        else:
            row.append("禁用")

        ws.append(row)

version = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
print (version)
wb.save("导出所有设备报备后台应用账号列表-版本[%s].xlsx" % version)
wb.close()